<?php

function mycurl($data,$url){
	//open connection
	$ch = curl_init();
	//set the url, number of POST vars, POST data
	curl_setopt($ch,CURLOPT_URL, $url);
	curl_setopt($ch,CURLOPT_POST, count($data));
	curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);

	//execute post
	$result = curl_exec($ch);
	return $result;
	//close connection
	curl_close($ch);
}
$re=file_get_contents("http://manga24h.com/no-cache/chap2.php?all&time=".date('U'));
echo "start nao \n";
$re=unserialize($re);


//Start foreach chapter
$k=1;
foreach($re as $val){
	//$val co id,manga,bk
	echo "Chap ".$k++."   ------ ".$val['id'];
	//echo $val['id'].":".$val['chap_id']." <Br/>";
	//Get detail chapter from manga24h
	$re2=file_get_contents("http://manga24h.com/no-cache/chap2.php?detail&id=".$val['id']);
	
	//Remote to picasa upload--------------
	$row=unserialize($re2);
	//------Fetch chapter content
	$dt=unserialize($row['chap_content']);
				
					print_r($dt);
							$j=0; //DAT TEN FILE IMAGE
							$e=0; //Tinh tong link hong
							$t =array();
							foreach($dt as $val2){
								
								$val2=str_replace(" ",'%20',$val2);
							
								$size=@getimagesize($val2);
								if(!$size){
								 //Image hong roi
									$e++;
									
								}else{
									
									//Neu ko phai la link blogspot thi moi download
									$path="data/".$val['manga']."/".$val['id'];
									if(!is_dir($path)){
												mkdir($path,0775,true);
									}
											$content = file_get_contents($val2);
												if(file_exists($path."/".$j.".jpg")){
													unlink($path."/".$j.".jpg");
												}
											file_put_contents($path."/".$j.'.jpg', $content);
											$t[]="http://bk.manga24h.com/".$path."/".$j.'.jpg';
								
									
								//	echo $j.'.jpg Done /n ';
								
								}
								$j++;
							}
							
						
						
							

				
					
					if(count($t)>0){
						$link=serialize($t);
					}else{
						$link='';
					}
				
					
					$result=array("error"=>$e,
								"picasa"=>$link
							);
				
					
								  
		
	
	
	
	//End remote upload to Picasa -------
	
	//Send content update with id
	
	$result=serialize($result);
	$data['content']=urlencode($result);
	$result=mycurl($data,'http://manga24h.com/no-cache/chap2.php?update&manga='.$val['manga'].'&chap_id='.$val['id']);
	echo $result;
	
} //End foreach chapter

